package top.wardyan.wbaseswt.conf.config;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.wall.WallConfig;
import com.alibaba.druid.wall.WallFilter;
import com.jfinal.aop.Aop;
import com.jfinal.config.Plugins;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.dialect.SqlServerDialect;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.template.source.ClassPathSourceFactory;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.log.Log;
import top.wardyan.base.common.annotation.scanner.ServiceScanner;
import top.wardyan.base.common.annotation.scanner.TableScanner;
import top.wardyan.base.common.db.test.IKtDeviceService;
import top.wardyan.base.common.db.test.KtDevice;

/**
 * 扫描数据库配置文件,自动打开
 * @author harrydeep@wardyan.top
 * 2023年5月1日
 */
public class AppConfigScanner {
	
	private static final String PREFIX = "dbconfig";
	private static final String POSTFIX = ".txt";
	private static final String URL = "jdbc:sqlite://";
	private static final String FILENAME = "conf/AppConfig.db";
	private static final String CONFIGNAME = "AppConfig";
	private static final Log log = Log.get();
	
	
	public static void createSQLiteConnection() {
		
		
		
		long before = System.currentTimeMillis();

		File file = new File(FILENAME);
		String url = URL + file.getAbsolutePath();
		log.info("初始化数据库连接:{} {}", CONFIGNAME, url);
		
		DruidPlugin druidPlugin = new DruidPlugin(url, "", "");
		
		
		
		// 加强数据库安全
		WallFilter wallFilter = new WallFilter();
			
		wallFilter.setDbType("sqlite");

		

		druidPlugin.addFilter(wallFilter);
		// 添加 StatFilter 才会有统计数据
		//druidPlugin.addFilter(new StatFilter());
		
		ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
		
		// arp.setShowSql(true);
		// arp.setShowSql(PropKit.getBoolean("debug", false));
		//arp.setShowSql(PropKit.getBoolean("debug", false));
		arp.setDialect(new SqlServerDialect());
		//arp.addSqlTemplate("dock.sql");
		 
        arp.getEngine().setSourceFactory(new ClassPathSourceFactory());
        // arp.setContainerFactory(new CaseInsensitiveContainerFactory(true)); 
                
        // sql文件路径在src/mian/resources/sql/all_sqls.sql
        //arp.addSqlTemplate("/sql/all_sqls.sql");
        		
        //	arp.addMapping("blog", Blog.class);
        
        TableScanner tableScanner=new TableScanner("top.wardyan",arp, CONFIGNAME);
		tableScanner.scan();
		
		// 也扫描下这个
		TableScanner tableScanner2=new TableScanner("com.uuerp",arp, CONFIGNAME);
		tableScanner2.scan();
		
		


		// 扫描service
		ServiceScanner serviceScanner = new ServiceScanner("top.wardyan");
		serviceScanner.scan();
		ServiceScanner serviceScanner2 = new ServiceScanner("com.uuerp");
		serviceScanner2.scan();
		
		
		
		
        	    
        // 与 jfinal web 环境唯一的不同是要手动调用一次相关插件的start()方法
	
		druidPlugin.start();
        arp.start();
		
        //EhCachePlugin ec = new EhCachePlugin();
        //ec.start();
        // CacheKit
        // System.out.println("inited");
       

		log.info("初始化数据库连接成功:{} {} 耗时(ms):{}", CONFIGNAME, url, System.currentTimeMillis() - before);
		
	}
	
	
	/**
	 * 扫描配置文件并建立数据库连接
	 * 配置文件命名规则:dbconfig*.txt
	 * 配置文件内容:
	 * configName=配置名
	 * dbType=数据库类型 1sqlserver
	 * host=
	 * port=
	 * dbName=
	 * user=
	 * password=
	 * 配置文件后面需要用一个特殊程序来生成
	 * 至于选项,后面再搞一个列表
	 */
	public static void scan(Plugins me) {
		
		AppConfigScanner.createSQLiteConnection();
				
	}
	
	
//	/***
//	 * 列出所有数据库配置
//	 * @return
//	 */
//	public static List<Prop> listConfigFiles(){
//		List<Prop> rs = new ArrayList<>();
//		
//		List<String> files = FileUtil.listFileNames("");		
//		
////		File file = FileUtil.newFile("202130444444444.txt");
////		
////		FileUtil.writeString("123", file, Charset.forName("utf8"));
//		
//		// Console.log(files);
//		if(files == null) {
//			files = new ArrayList<>();
//		}
//		Console.log("DBConfigScanner", files);
//		for(String name : files) {
//			
//			if(name.startsWith(PREFIX) && name.endsWith(POSTFIX)) {
//				rs.add(PropKit.use(name));
//			}
//			
//		}
//		
//		return rs;
//	}
	
	public static void main(String[] args) {
		// DBConfigScanner scanner = new DBConfigScanner();
		// Console.log(scanner.listConfigFiles());
		scan(null);
//		
//		
//		IKtDeviceService deviceService = Aop.get(IKtDeviceService.class);
//		KtDevice ls = deviceService.findBySQL("select * from kt_device");
//		Console.log("DBConfigScanner", ls);
		
	}

}
